#include "Font.h"

Font* g_pFont;
Font* g_pWeaponFont;

Font* Font::BuildFont( const char* szFace, int iSize, int iWeight, int iFlags )
{
	Font* pFont = new Font;

	if( !pFont )
	{
		return NULL;
	}

	pFont->m_id = g_pSurface.CreateFont();

	if( !pFont->m_id )
	{
		SAFE_DELETE( pFont );
		return NULL;
	}

	if( !g_pSurface.SetFontGlyphSet( pFont->m_id, szFace, iSize, iWeight, 0, 0, iFlags ) )
	{
		SAFE_DELETE( pFont );
		return NULL;
	}

	pFont->m_size = iSize;
	return pFont;
}

void Font::PrintString( int x, int y, int r, int g, int b, int a, DWORD_PTR dwAlign, const char* szText, ... )
{
	va_list		va_alist;
	char		szBuf[1024];
	int			iLen;
	int			iWidth;
	int			iHeight;	

	va_start( va_alist, szText );
	_vsnprintf( szBuf, sizeof( szBuf ), szText, va_alist );
	va_end( va_alist );

	iLen = strlen( szBuf );

	wchar_t* pszFmt = new wchar_t[ iLen+1 ];
	mbstowcs( pszFmt, szBuf, iLen+1 );

	g_pSurface.GetTextSize( m_id, pszFmt, iWidth, iHeight );

	if( dwAlign & FA_Right )
		x -= iWidth;
	else if( dwAlign & FA_CenterW )
		x -= iWidth/2;
	if( dwAlign & FA_CenterH )
		y -= iHeight/2;

	g_pSurface.DrawSetTextColour( r, g, b, a );
	g_pSurface.DrawSetTextFont( m_id );
	g_pSurface.DrawSetTextPos( x, y );
	g_pSurface.DrawPrintText( pszFmt, iLen );
	SAFE_DELETE_ARRAY( pszFmt );
}